from datetime import datetime
from flask_login import UserMixin
from routes import db, login_manager
# from sqlalchemy import Integer, String, BLOB, TIMESTAMP
# from sqlalchemy.orm import Mapped, mapped_column
from bcrypt import hashpw, gensalt

@login_manager.user_loader
def load_user(user_id):
    return User.query.get(int(user_id))

class User(db.Model, UserMixin):
    __tablename__ = 'user_info'
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(50), unique=True, nullable=False)
    password_hash = db.Column(db.String(100), nullable=False)
    email = db.Column(db.String(100), unique=True, nullable=True)

    def __repr__(self):
        return f"User('{self.username}', '{self.email}')"
    # 用关键字参数创建对象
    def __init__(self, username, password, email=None):
        self.username = username
        self.email = email
        self.set_password(password)  # 自动设置密码哈希
    def to_dict(self):
        return {
            'id': self.id,
            'username': self.username,
            'email': self.email
        }

    def set_password(self, password):
        self.password_hash = hashpw(password.encode(), gensalt()).decode()
        # from werkzeug.security import generate_password_hash
        # self.password_hash = generate_password_hash(password)

    def check_password(self, password):
        return hashpw(password.encode(), self.password_hash.encode()) == self.password_hash.encode()


class RegulationData(db.Model):
    __tablename__ = 'regulation_services'
    id = db.Column(db.Integer, primary_key=True)
    year = db.Column(db.Integer, nullable=False)
    gepCity = db.Column(db.String(50), nullable=False)
    gepDistrict = db.Column(db.String(50), nullable=False)
    gepTown = db.Column(db.String(50), nullable=False)
    outWaterSourceConservation = db.Column(db.Float, nullable=True)
    outSoilConservation = db.Column(db.Float, nullable=True)
    outNegativeOxygenIons = db.Column(db.Float, nullable=True)
    outAirPurification = db.Column(db.Float, nullable=True)
    outWaterPurification = db.Column(db.Float, nullable=True)
    outCarbonSequestration = db.Column(db.Float, nullable=True)
    outOxygenRelease = db.Column(db.Float, nullable=True)
    outClimateRegulation = db.Column(db.Float, nullable=True)
    outCalcFloodRegulation = db.Column(db.Float, nullable=True)
    outCalcBiodiversityProtection = db.Column(db.Float, nullable=True)
    outCalcNoiseReduced = db.Column(db.Float, nullable=True)
    creator = db.Column(db.String(50), nullable=False)
    updater = db.Column(db.String(50), nullable=False)
    created_at = db.Column(db.DateTime, default=datetime.utcnow)
    updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)

    def to_dict(self):
        return {
            'id': self.id,
            'year': self.year,
            'gepCity': self.gepCity,
            'gepDistrict': self.gepDistrict,
            'gepTown': self.gepTown,
            'outWaterSourceConservation': self.outWaterSourceConservation,
            'outSoilConservation': self.outSoilConservation,
            'outNegativeOxygenIons': self.outNegativeOxygenIons,
            'outAirPurification': self.outAirPurification,
            'outWaterPurification': self.outWaterPurification,
            'outCarbonSequestration': self.outCarbonSequestration,
            'outOxygenRelease': self.outOxygenRelease,
            'outClimateRegulation': self.outClimateRegulation,
            'outCalcFloodRegulation': self.outCalcFloodRegulation,
            'outCalcBiodiversityProtection': self.outCalcBiodiversityProtection,
            'outCalcNoiseReduced': self.outCalcNoiseReduced,
            'creator': self.creator,
            'updater': self.updater,
            'created_at': self.created_at.isoformat(),
            'updated_at': self.updated_at.isoformat()
        }
